#pragma once

#include "VConfigs.hpp"
#include "VBlock.hpp"
#include "Gesture.hpp"

class VPlayAreaPan;
class VPlayBlocks;
class VPlayScoreBar;

class VPlayArea : public Zen::Vap::Combination2D {
public:
	virtual void load() override {
		this->loadView();
	}

	void loadView();

//	virtual Node * touchDown(AppTouch const & touch) override; // { return nullptr; }
//	virtual Node * touchMove(AppTouch const & touch) override { return this; }
//	virtual Node * touchUp(AppTouch const & touch) override { return nullptr; }
//	virtual Node * touchCancel(AppTouch const & touch) override { return nullptr; }

//	void updateMap(Map const & map);

	void setCtrlUpdate(std::function<void(float delay)> onTick);

	void setGestureEvent(std::function<void(GestureType)> onEvent);

	void updateNextTetris(TetrisInfo info);

	void updateCurrentTetris(TetrisInfo info, Pos pos);

	void updateMap(Map const & map);

	void updateScore(int score);

	void checkResult(Result const & result, std::function<void()> onDone);

	void runFaild(std::function<void()> onDone);

	void restartGame(std::function<void()> onDone);

	VPlayBlocks * getPlayBlocks() { return m_play_blocks; }

	void onUpdate(float);
protected:
	VPlayAreaPan * m_pan;
	VPlayScoreBar * m_score_bar;
	VPlayBlocks * m_play_blocks;
	VTetrisBlocks * m_current_tetris;
	VTetrisBlocks * m_next_tetris;
	Vec2 m_blocks_position;

	std::function<void(float)> m_ctrl_update;
	Gesture * m_gesture = nullptr;
};

class VPlayBlocks : public Combination2D {
public:
	LoadViewDeclare();

	void clearLines(Result const & result, std::function<void()> onDone);
	
	void resetBlocks(Map const & map);

	Vec2 getSubPosition(int x, int y);
protected:
	VBlock * m_blocks[GMapHeight][GMapWidth];
	float m_block_view_width;
	Vec2 m_block_view_lb;
};

class VPlayAreaPan : public Zen::Vap::Combination2D {
public:
	LoadViewDeclare();
	Vec2 getPosMove() { return m_pos_move; }
protected:
	Sprite * m_pan_bg;
	Vec2 m_pos_move;
};

class VPlayScoreBar : public Combination2D {
public:
	LoadViewDeclare();
	void setNextTetris(TetrisInfo info);
	void setScore(int score);

//	FontLabel * m_score_lb;
	TilemapLabel * m_score_lb;
	int m_score_value = 0;
};
